package com.boarsoft.boar.soagov.svc.biz;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.entity.FlowInfo;
import com.boarsoft.boar.entity.ObjFaces;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.boar.entity.SvcInfo;
import com.boarsoft.boar.soagov.ObjFacesBiz;
import com.boarsoft.boar.soagov.com.ComBiz;
import com.boarsoft.boar.soagov.nexus.NexusBiz;
import com.boarsoft.boar.soagov.svc.SvcBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("svcBiz")
public class SvcBizImpl extends SimpleBizImpl implements SvcBiz {
	@Autowired
	private ComBiz comBiz;
	@Autowired
	private ObjFacesBiz objFacesBiz;
	@Autowired
	private NexusBiz nexusBiz;

	@Override
	@Transactional
	public ReplyInfo<Object> delete(String id) {
		ReplyInfo<Object> ri = comBiz.delete(id);
		if (ri.isSuccess()) {
			dao.delete(SvcInfo.class, id);
		}
		return ri;
	}

	@Override
	@Transactional
	public boolean status(String id, short status) {
		SvcInfo o = dao.get(SvcInfo.class, id);
		if (status == 1) {
			o.setStatus(SvcInfo.STATUS_DISABLE);
		} else {
			o.setStatus(SvcInfo.STATUS_ENABLE);
		}
		dao.update(o);
		return true;
	}

	@Override
	@Transactional(readOnly = true)
	public SvcInfo get(String id) {
		return (SvcInfo) dao.get(SvcInfo.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<SvcInfo> list(String projId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (s.code like '%").append(key);
			sb.append("%' or s.name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(SvcInfo.class, sb.toString());
		List<SvcInfo> lt = dao.list(SvcInfo.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<SvcInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public String copy(String sourceId, String parentId, String newId) {
		// copy SvcInfo
		SvcInfo n = this.get(sourceId);
		dao.evict(n);
		n.setId(newId);
		dao.save(n);
		// copy FlowInfo
		FlowInfo fo = dao.get(FlowInfo.class, sourceId);
		if (fo != null) {
			dao.evict(fo);
			fo.setId(newId);
			dao.save(fo);
		}
		// copy ObjFaces，复制“我”依赖的接口
		List<ObjFaces> ofLt = objFacesBiz.list(sourceId);
		for (ObjFaces of : ofLt) {
			if (of.getFace() == null) {
				log.warn("Wrong object-face relation between object {}/{} and face {}", of.getObjType(), of.getObjId(),
						of.getFaceId());
				continue;
			}
			dao.evict(of);
			of.setId(null);
			of.setObjId(n.getId());
			dao.save(of);
		}
		// copy SoaNexus，只复制“我的依赖”，不复制“依赖我的”
		List<SoaNexus> snLt = nexusBiz.listByMe(sourceId);
		for (SoaNexus sn : snLt) {
			dao.evict(sn);
			sn.setId(null);
			sn.setObjId0(n.getId());
			dao.save(sn);
		}
		return n.getId();
	}

	@Override
	public boolean move(String id, String to) {
		return true;
	}

	@Override
	@Transactional
	public void save(SvcInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}
}